//
// Created by bjliuyong on 2021/7/13.
//

#ifndef JVM_OSTREAM_H
#define JVM_OSTREAM_H

#include "globalDefinitions.h"

class outputStream {
protected:

    int _indentation; // current indentation
    int _width;       // width of the page
    int _position;    // position on the current line
    int _newlines;    // number of '\n' output so far
    julong _precount; // number of chars output, less _position

public:

    // creation
    outputStream(int width = 80);

    void print_raw(const char *str) { write(str, strlen(str)); }

    // flushing
    virtual void flush() {}
    virtual void write(const char* str, size_t len) = 0;

};

// for writing to strings; buffer will expand automatically
class stringStream : public outputStream {
protected:
    char *buffer;
    size_t buffer_pos;
    size_t buffer_length;
    bool buffer_fixed;
    //DEBUG_ONLY(ResourceMark* rm;)
public:
    stringStream(size_t initial_bufsize = 256);

    stringStream(char *fixed_buffer, size_t fixed_buffer_size);

    ~stringStream();

    virtual void write(const char *c, size_t len);

    size_t size() { return buffer_pos; }

    const char *base() { return buffer; }

    void reset() {
        buffer_pos = 0;
        _precount = 0;
        _position = 0;
    }

    char *as_string();
};

#endif //JVM_OSTREAM_H
